import 'package:cbec_app/components/base/base_image.dart';
import 'package:cbec_app/components/base/base_interactive.dart';
import 'package:cbec_app/components/base/base_page.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/base/base_nav_bar.dart';
import 'package:cbec_app/components/order/promotions_order_item.dart';
import 'package:cbec_app/pages/my/controller/my_promotions_ctr.dart';
import 'package:cbec_app/routes/routes.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:intl/intl.dart';

//我的推广
class MyPromotions extends StatelessWidget {
  final MyPromotionsController mpCtr = Get.put(MyPromotionsController());

  final DateFormat format = DateFormat('yyyy-MM-dd');

  Future<void> saveImage(String imagePath) async {
    var response = await Dio()
        .get(mpCtr.fileUrl, options: Options(responseType: ResponseType.bytes));
    final result = await ImageGallerySaver.saveImage(
        Uint8List.fromList(response.data),
        quality: 60,
        name: "hello");
    if (result['isSuccess'] == true) {
      Tools.showToast("保存成功,请前往相册查看");
    } else {
      Tools.showToast("保存失败,请稍后重试");
    }
    // print(result);
  }

  MyPromotions({super.key});

  final TextStyle subtitle =
      TextStyle(fontSize: 12, color: Tools.fromHex("#999999"));
  final TextStyle numberStyle = TextStyle(
      fontSize: 15,
      color: Tools.fromHex("#333333"),
      fontWeight: FontWeight.bold);

  @override
  Widget build(BuildContext context) {
    return BasePage(
      title: "我的推广",
      // bgColor: Tools.fromHex("#FD9778"),
      bgImageUrl: "my_promotions_bg.png",
      bodyColor: Colors.transparent,
      iconType: IconType.back,
      children: [
        Container(
          margin: EdgeInsets.all(18.w),
          height: 180.h,
          decoration: border_radis(Colors.white),
          child: Column(
            children: [
              SizedBox(
                height: 31.h,
              ),
              Text("本月销售额累计(元)", style: subtitle),
              BaseInteractive(
                onClick: () => {
                  Get.toNamed(Routes.salesData)
                },
                childWidget: Text(
                  "52,150",
                  style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
                ),
              ),
              SizedBox(
                height: 20.h,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Column(
                    children: [
                      Text("本月订单数", style: subtitle),
                      SizedBox(
                        height: 4.h,
                      ),
                      Text("100", style: numberStyle)
                    ],
                  ),
                  Column(
                    children: [
                      Text("累计销售总额(元)", style: subtitle),
                      SizedBox(
                        height: 4.h,
                      ),
                      Text("1,205,35", style: numberStyle)
                    ],
                  ),
                  Column(
                    children: [
                      Text("累计订单数", style: subtitle),
                      SizedBox(
                        height: 4.h,
                      ),
                      Text("3,155", style: numberStyle)
                    ],
                  ),
                ],
              )
            ],
          ),
        ),
        //中间
        Container(
          // height: 85.h,
          margin: EdgeInsets.fromLTRB(18.w, 20.w, 18.w, 10.w),
          decoration: border_radis(Colors.white),
          padding: EdgeInsets.all(
            10.w,
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                "本月拉新人数",
                style: TextStyle(fontSize: 13),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text("356", style: TextStyle(fontSize: 12)),
                  BaseInteractive(
                    onClick: () => {
                      showModalBottomSheet(
                          context: context,
                          builder: (BuildContext context) {
                            return Container(
                              width: double.infinity,
                              height: 335.h,
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  GestureDetector(
                                    onLongPress: () =>
                                        {saveImage(mpCtr.fileUrl)},
                                    child: BaseImage(
                                        url: mpCtr.fileUrl,
                                        width: 180,
                                        height: 180),
                                  ),
                                  SizedBox(height: 20.w),
                                  Text("长按保存到相册")
                                ],
                              ),
                            );
                          })
                    },
                    childWidget: Container(
                      padding: EdgeInsets.only(
                          left: 10.w, right: 10.w, top: 5.w, bottom: 5.w),
                      height: 30.h,
                      decoration: border_radis(Tools.fromHex("#F3F5F7"),
                          radiusValue: 20),
                      child: Row(
                        children: [
                          Icon(Icons.qr_code),
                          SizedBox(
                            width: 2.w,
                          ),
                          Text("我的邀请码", style: TextStyle(fontSize: 12))
                        ],
                      ),
                    ),
                  )
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Text("累计拉新人数:",
                      style: TextStyle(
                          fontSize: 12, color: Tools.fromHex("#999999"))),
                  Text("5623", style: TextStyle(fontSize: 12))
                ],
              ),
            ],
          ),
        ),

        //下方的客户列表及客户订单

        Container(
          color: Colors.white,
          child: Column(
            children: [
              Container(
                child: TabBar(
                  controller: mpCtr.tabController,
                  indicatorColor: Tools.fromHex("#F83F2D"),
                  unselectedLabelStyle: TextStyle(fontSize: 12),
                  labelStyle: TextStyle(
                      fontSize: 14,
                      fontWeight: FontWeight.bold,
                      color: Tools.fromHex("#F83F2D")),
                  tabs: [
                    Tab(text: '客户列表'),
                    Tab(text: '客户订单'),
                  ],
                ),
              ),
            ],
          ),
        ),

        //列表部分

        Container(
          height: 350.h,
          child: TabBarView(controller: mpCtr.tabController, children: [
            Container(
                color: Colors.white,
                child: Column(
                  children: [
                    _customTop(context),
                    Container(
                      color: Tools.fromHex("#F3F5F7"),
                      padding:
                          EdgeInsets.only(left: 8.w, right: 8.w, bottom: 20.w),
                      height: 300.h,
                      child: ListView(
                        children: [
                          customeCardItem(),
                          customeCardItem(),
                          customeCardItem(),
                          customeCardItem(),
                        ],
                      ),
                    )
                  ],
                )),
            Container(
              color: Colors.white,
              padding: EdgeInsets.only(left: 8.w, right: 8.w),
              height: 300.h,
              child: Column(
                children: [
                  _orderTop(),
                  Container(
                    color: Tools.fromHex("#F3F5F7"),
                    padding:
                        EdgeInsets.only(left: 8.w, right: 8.w, bottom: 20.w),
                    height: 300.h,
                    child: ListView(
                      children: [
                        PromotionsOrderItem(),
                        PromotionsOrderItem(),
                        PromotionsOrderItem(),
                      ],
                    ),
                  )
                ],
              ),
            ),
          ]),
        )
      ],
    );
  }

  Widget _orderTop() {
    return Container(
      child: TabBar(
        controller: mpCtr.orderTabController,
        tabs: [
          Tab(text: '全部'),
          Tab(text: '待发货'),
          Tab(text: '已发货'),
          Tab(text: '已完成'),
        ],
        indicator: const BoxDecoration(),
        labelStyle: TextStyle(
            fontSize: 14,
            fontWeight: FontWeight.bold,
            color: Tools.fromHex("#333333")),
        unselectedLabelStyle:
            TextStyle(fontSize: 14, color: Tools.fromHex("#999999")),
      ),
    );
  }

  //客户列表的筛选
  Widget _customTop(BuildContext context) {
    return Container(
      height: 41.h,
      padding: EdgeInsets.only(left: 19.w, right: 19.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              Text("全部"),
              BaseImage(url: "promotions_xl.png", width: 14, height: 14)
            ],
          ),
          Row(
            children: [
              Text("下单时间"),
              SizedBox(
                width: 20.w,
              ),
              BaseInteractive(
                onClick: () => {_show(context)},
                childWidget: Container(
                  padding: EdgeInsets.fromLTRB(10.w, 2.w, 10.w, 2.w),
                  decoration:
                      border_radis(Tools.fromHex("#F3F5F7"), radiusValue: 20),
                  child: Row(
                    children: [
                      Text("时间区间"),
                      BaseImage(url: "promotions_xl.png", width: 14, height: 14)
                    ],
                  ),
                ),
              )
            ],
          )
        ],
      ),
    );
  }

  //日期范围选择器
  void _show(BuildContext context) async {
    DateTime firstDate = DateTime(2021, 1, 1);
    DateTime lastDate = DateTime.now();
    DateTime start = lastDate.add(const Duration(days: -8));
    DateTime end = lastDate.add(const Duration(days: -2));
    DateTimeRange? range = await showDateRangePicker(
        context: context,
        firstDate: firstDate,
        lastDate: lastDate,
        initialDateRange: DateTimeRange(
          start: start,
          end: end,
        ),
        saveText: "确定",
        builder: (context, child) {
          return Theme(
            data: ThemeData.light().copyWith(
              colorScheme: ColorScheme.light().copyWith(
                primary: Colors.red, // 设置选中的日期为红色
              ),
            ),
            child: child!,
          );
        });
    if (range != null) {
      print("${format.format(range.start)} ~ ${format.format(range.end)}");
    }
  }
}

class customeCardItem extends StatelessWidget {
  const customeCardItem({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: border_radis(Colors.white),
      margin: EdgeInsets.only(top: 10.w),
      child: Column(
        children: [
          //上
          Container(
            decoration: bottom_gary_border,
            padding: EdgeInsets.only(
                left: 10.w, top: 10.w, right: 23.w, bottom: 10.w),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Row(
                  children: [
                    BaseImage(
                        url:
                            "https://q9.itc.cn/q_70/images03/20240312/5c2f7c3ad5eb4d2fb8fb5fe5ae92e245.jpeg",
                        width: 44,
                        height: 44,
                        borderRadius: BorderRadiusDirectional.circular(22)),
                    SizedBox(
                      width: 10.w,
                    ),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            Text("李小白",
                                style: TextStyle(
                                    fontSize: 13, fontWeight: FontWeight.bold)),
                            SizedBox(width: 10.w),
                            Text("18215533693",
                                style: TextStyle(
                                    fontSize: 13, fontWeight: FontWeight.bold))
                          ],
                        ),
                        SizedBox(
                          height: 4.w,
                        ),
                        Text("权益会员卡：230天到期", style: TextStyle(fontSize: 12))
                      ],
                    ),
                  ],
                ),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Text("下单金额:￥238", style: TextStyle(fontSize: 12)),
                    SizedBox(
                      height: 4.w,
                    ),
                    Text("订单数：12", style: TextStyle(fontSize: 12))
                  ],
                )
              ],
            ),
          ),
          //下
          Container(
            padding: EdgeInsets.only(left: 10.w),
            height: 38.h,
            child: Row(
              children: [
                Text("绑定时间:",
                    style: TextStyle(
                        fontSize: 13, color: Tools.fromHex("#999999"))),
                SizedBox(
                  width: 3.w,
                ),
                Text("2024-30-26 08:59:39", style: TextStyle(fontSize: 13))
              ],
            ),
          )
        ],
      ),
    );
  }
}
